<template>
  <div class="user-management-container">
    <el-form ref="form" label-width="200px" :model="form" :rules="rules">
      <el-form-item label="区域名称" prop="areaName">
        <el-input v-model="form.areaName" placeholder="请输入区域名称" />
      </el-form-item>
    </el-form>
    <el-button class="baocun" size="medium" type="primary" @click="save">
      保存
    </el-button>
  </div>
  <!-- 增加 -->
</template>

<script>
  /* eslint-disable*/
  import { doAdd } from '@/api/diqu'
  import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
  } from 'element-china-area-data'
  //import { number } from 'echarts'
  export default {
    name: 'search',

    data() {
      // 手机号码验证
      var contactPhone = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('手机号不能为空'))
        } else {
          const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
          if (reg.test(value)) {
            callback()
          } else {
            return callback(new Error('请输入正确的手机号'))
          }
        }
      }
      const validatesurfacePlot = (rule, value, callback) => {
        console.log(this.form.enterpriseLicense)
        if (this.form.enterpriseLicense === '') {
          callback(new Error('请添加图片!!!!'))
        } else {
          callback()
        }
      }
      return {
        options: regionDataPlus,
        selectedOptions: [],
        token: '',
        imageUrl: '',
        value: true,
        value1: true,
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        options1: [],
        options2: [],
        options3: [],
        valueone: '',
        valuetwo: '',
        form: {
          areaName: '',
        },
        activated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.play()
        },
        deactivated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.pause()
        },

        rules: {
          areaName: [
            { required: true, trigger: 'blur', message: '请输入区域名称' },
          ],

          linkmans: [
            { required: true, trigger: 'blur', message: '请输入联系人' },
          ],
          enterpriseLicense: [
            { required: true, trigger: 'change', message: '请添加图片' },
            { validator: validatesurfacePlot, trigger: 'change' },
          ],
          enterpriseName: [
            { required: true, message: '请输入企业名称', trigger: 'blur' },
          ],
          leaderIntro: [
            { required: true, trigger: 'blur', message: '请输入领队简介' },
          ],
          cityId: [
            { required: true, trigger: 'change', message: '请选择城市' },
          ],
          areaId: [
            { required: true, trigger: 'change', message: '请选择区域' },
          ],
          linkphone: [
            {
              required: true,
              trigger: 'blur',
              validator: contactPhone,
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.token = this.$store.getters['user/token']
    },
    methods: {
      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.surfacePlot = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      choose(e) {
        this.form.crewTheme = e.themeName
        this.form.crewThemeId = e.id
      }, //选择区域
      chooseone(e) {
        this.form.areaName = e.areaName
        this.form.areaId = e.id
      },
      handleChange(a) {
        this.form.particulars = a
      },
      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.enterpriseLicense = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.imageUrl = row.enterpriseLicense
        }
        this.dialogFormVisible = true
      },
      close() {
        let userId = this.form.userId
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        if (!this.form.id) {
          this.form.userId = userId
        }
        console.log(this.form)
        this.dialogFormVisible = false
        this.imageUrl = ''
        // this.form = ''
      },
      save() {
        // this.$router.push('/other/workflow')

        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              const msg = doEdit(this.form)
              console.log(msg)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              this.$emit('fetch-data')
              this.close()
            } else {
              const msg = doAdd(this.form)
              this.$emit('fetch-data')
              this.close()
              msg.then((res) => {
                console.log(res.msg)
                if (res.msg === 'success') {
                  this.$baseMessage(
                    '添加成功',
                    'success',
                    'vab-hey-message-success'
                  )
                  this.$router.push({ path: '/setting/diqu/diqulist' })
                }
              })
            }
          }
        })
      },
      nickNameChange() {
        this.$forceUpdate()
      },
      /**省市区三级联动 */
      addressChoose(value) {
        this.form.cityId = value[2]
        this.form.cityName =
          CodeToText[value[0]] +
          '/' +
          CodeToText[value[1]] +
          '/' +
          CodeToText[value[2]]
      },
    },
  }
</script>
<style>
  /* .huakuai {
    position: relative;
    left: 96%;
  } */
  .avatar-uploader .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    width: 178px;
    height: 178px;
    font-size: 28px;
    line-height: 178px;
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
    width: 178px;
    height: 178px;
  }
  .baocun {
    width: 100%;
    height: 100%;
  }
</style>
